<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="对账时间" prop="accountDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.accountDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择对账时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="对账状态" prop="accountSts">
        <el-input
          v-model="queryParams.accountSts"
          placeholder="请输入对账状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="缴费渠道" prop="costWay">
        <el-input
          v-model="queryParams.costWay"
          placeholder="请输入缴费渠道"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="requestFile"
        >请求文件
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="checkAccount"
        >重新对账
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="对账时间" align="center" prop="accountDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accountDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账状态" align="center" prop="accountSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.COST003" :value="scope.row.accountSts"/>
        </template>
      </el-table-column>
      <el-table-column label="缴费渠道" align="center" prop="costWay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.COST002" :value="scope.row.costWay"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改对账对话框 -->
    <el-dialog :title="titleProjectExecute" :visible.sync="openProjectExecute" append-to-body width="500px">
      <el-form ref="form" :model="formProjectExecute" :rules="rules" label-width="80px">
        <el-form-item label="对账时间" prop="datetime">
          <el-date-picker v-model="formProjectExecute.datetime"
                          clearable
                          placeholder="选择对账时间"
                          type="date" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProjectExecuteForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="titleDateTime" :visible.sync="openDateTime" append-to-body width="500px">
      <el-form ref="form" :model="formDateTime" :rules="rules" label-width="80px">
        <el-form-item label="请求文件时间" prop="datetime">
          <el-date-picker v-model="formDateTime.datetime"
                          clearable
                          placeholder="选择请求时间"
                          type="date" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDateTimeForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listT, getT,requestFile,checkAccount} from "@/api/fund/account";

export default {
  name: "account",
  dicts: ['COST003', 'COST002', 'COST001'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账表格数据
      tList: [],
      titleProjectExecute: "",
      titleDateTime: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDateTime: false,
      openProjectExecute: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountDate: null,
        accountSts: null,
        costWay: null
      },
      // 表单参数
      form: {},
      formProjectExecute: {},
      formDateTime: {},
      // 表单校验
      rules: {
        accountDate: [
          { required: true, message: "对账时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /*    requestFile() {
      return requestFile().then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },*/
    requestFile(row) {
      this.reset();
      this.openDateTime = true;
      this.titleDateTime = "请求时间";
    },
    checkAccount(row) {
      this.reset();
      this.openProjectExecute = true;
      this.titleProjectExecute = "对账时间";
    },
    /** 请求按钮操作 */
    submitDateTimeForm() {
      requestFile(this.formDateTime.datetime).then(response => {
        this.$modal.msgSuccess("请求成功");
        this.openDateTime = false;
        this.getList();
      });
    },
    /** 对账按钮操作 */
    submitProjectExecuteForm() {
      checkAccount(this.formProjectExecute.datetime).then(response => {
        this.$modal.msgSuccess("对账成功");
        this.openProjectExecute = false;
        this.getList();
      });
    },
    /** 查询对账列表 */
    getList() {
      this.loading = true;
      listT(this.queryParams).then(response => {
        this.tList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openProjectExecute = false;
      this.openDateTime = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accountDate: null,
        accountSts: null,
        costWay: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateT(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addT(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fund/account/export', {
        ...this.queryParams
      }, `t_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
